package com.fun.wyunfei.timeline.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Date工具
 * create by author wyunfei on 2018/9/19 10
 */
@SuppressWarnings({"unused", "WeakerAccess"})
public class DateUtil {

    public static final SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    public static final SimpleDateFormat DF2 = new SimpleDateFormat("yyyy-MM-dd HH", Locale.CHINA);
    public static final SimpleDateFormat DF3 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
    public static final SimpleDateFormat DF4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    private static Calendar cal = Calendar.getInstance();

    /**
     * @return yyyy-MM-dd
     */
    public static String getDateWithDay() {
        return DF1.format(new Date());
    }

    /**
     * @return yyyy-MM-dd HH
     */
    public static String getDateWithHour() {
        return DF2.format(new Date());
    }

    /**
     * @return yyyy-MM-dd HH:mm
     */
    public static String getDateWithMinute() {
        return DF3.format(new Date());
    }

    /**
     * @param df SimpleDateFormat
     */
    public static String getDateWithDf(SimpleDateFormat df) {
        return df.format(new Date());
    }

    /**
     * 描述:获取下一个月.
     *
     * @return nextYear
     */
    public static String getPreMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
        return dft.format(cal.getTime());
    }


    /**
     * 计算时间相差天数
     */
    public static int betweenDay(Date before, Date after) {
        long between_days = getTimeDifference(before, after) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 计算时间相差小时数
     */
    public static int betweenHour(Date before, Date after) {
        long between_days = getTimeDifference(before, after) / (1000 * 3600);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 计算时间相差分钟数
     */
    public static int betweenMinute(Date before, Date after) {
        long between_days = getTimeDifference(before, after) / (1000 * 60);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * @param str like : yyyy-mm-dd...
     * @param df  SimpleDateFormat
     * @return Date
     * @throws ParseException ParseException
     */
    public static Date parseDate(String str, SimpleDateFormat df) throws ParseException {
        return df.parse(str);
    }

    /**
     * 时间差（毫秒数）
     */
    private static long getTimeDifference(Date before, Date after) {
        cal.setTime(before);
        long time1 = cal.getTimeInMillis();
        cal.setTime(after);
        long time2 = cal.getTimeInMillis();
        cal.setTime(new Date());
        return time2 - time1;
    }

    // test
//    public static void main(String[] args) throws ParseException {
//        System.out.println(DateUtil.getDateWithDay());
//        System.out.println(DateUtil.getDateWithHour());
//        System.out.println(DateUtil.getDateWithMinute());
//        System.out.println(DateUtil.getDateWithDf(DF4));
//        System.out.println(betweenDay(DF1.parse("2018-09-18"), new Date()));
//        System.out.println(betweenHour(DF1.parse("2018-09-18"), new Date()));
//        System.out.println(betweenMinute(DF1.parse("2018-09-18"), new Date()));
//    }

    /**
     * 下拉月份数据
     *
     * @param nowDates xxxx-xx
     */
    public static String[] makeDateList(String nowDates) {
        if (nowDates == null || nowDates.equals("") || nowDates.split("-").length != 2) {
            return new String[]{"时间转换错误！"};
        }
        int year = Integer.parseInt(nowDates.split("-")[0]);
        int month = Integer.parseInt(nowDates.split("-")[1]);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(year + "-" + month);
            if (month == 12) {
                year++;
                month = 1;
            } else
                month++;
        }
        String[] array = new String[list.size()];
        return list.toArray(array);
    }

    // 用于日期选择时间格式化为 yyyy-mm-dd
    public static String parseDf1Date(int year, int month, int dayOfMonth) {
        month = month + 1;
        String sb;
        if (month < 10) {
            sb = String.valueOf(year) + "-0" + month;
        } else
            sb = String.valueOf(year) + "-" + month;
        if (dayOfMonth < 10) {
            sb = sb + "-0" + dayOfMonth;
        } else
            sb = sb + "-" + dayOfMonth;
        return sb;
    }

    /**
     * 下拉月份（获取当前及以前）
     *
     * @param Date  yyyy-MM
     * @param count count
     * @return string...
     */
    public static String[] getBeforeMonthList(String Date, int count) {
        if (Date == null || Date.equals("") || Date.split("-").length != 2) {
            return new String[]{"时间转换错误！"};
        }
        int year = Integer.parseInt(Date.split("-")[0]);
        int month = Integer.parseInt(Date.split("-")[1]);
        List<String> list = new ArrayList<>();
        for (int i = count; i > 0; i--) {
            list.add(year + "-" + month);
            if (month == 1) {
                year--;
                month = 12;
            } else {
                month--;
            }
        }
        String[] array = new String[list.size()];
        return list.toArray(array);
    }

}
